package com.zpark.servlet;

import com.zpark.servlet.bena.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询所有用户数据
 */
@WebServlet("/find_user_all.do")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 将用户数据从数据库中查询出来，然后返回到页面做展示
         */
        User zs = new User();
        zs.setUsername("张三");
        zs.setPassword("123456");
        zs.setAge(23);
        zs.setSex("男");
        zs.setAddress("北京");

        User ls = new User();
        ls.setUsername("李四");
        ls.setPassword("12345678");
        ls.setAge(24);
        ls.setSex("男");
        ls.setAddress("上海");

        User ww = new User();
        ww.setUsername("王五");
        ww.setPassword("123489");
        ww.setAge(27);
        ww.setSex("男");
        ww.setAddress("广州");

        User dc = new User();
        dc.setUsername("大锤");
        dc.setPassword("12345789456");
        dc.setAge(45);
        dc.setSex("女");
        dc.setAddress("杭州");

        User gd = new User();
        gd.setUsername("狗蛋");
        gd.setPassword("001456");
        gd.setAge(78);
        gd.setSex("男");
        gd.setAddress("内蒙古");

        // 定义list集合，用于存放user对象
        List<User> users = new ArrayList<>();
        users.add(zs);
        users.add(ls);
        users.add(ww);
        users.add(dc);
        users.add(gd);

        // 将用户数据集合添加到request域当中
        request.setAttribute("users", users);

        // 转发到页面做展示
        request.getRequestDispatcher("/jsp/demo01.jsp").forward(request, response);

    }
}
